package com.softwareengineering.trafficapplicationv2;
import android.graphics.Color;

/**
 * This file is part of the Traffic Simulation Application.
 * The Traffic Simulation Application is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * The Traffic Simulation Application is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with the Traffic Simulation Application.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @file Car.java
 * @author (c) Martin Treiber, http://traffic-simulation.de/
 * @alterations David Black, Chris Nelson, and Garrett Stibbs, (c) December 03, 2012
 * 
 */

public class Car implements Moveable, Constants {

	private int color=Color.RED;
	public boolean isPerturbed=false;
	private int lane=0;
	public LaneChange lanechange;
	private int laneLast=lane;
	private double length=5.;
	public MicroModel model;
	private int nr;
	private double pos;
	private double realLane;
	public double tdelay=0.0;
	private double vel=10.0;

	public Car (Car carToCopy){
		this.pos = carToCopy.pos;
		this.vel = carToCopy.vel;
		this.lane = carToCopy.lane;
		this.model = carToCopy.model;
		this.lanechange = carToCopy.lanechange;
		this.length = carToCopy.length;
		this.color = carToCopy.color;
		this.nr = carToCopy.nr;
	}

	public Car (double x, double v, int lane, 
			MicroModel model, LaneChange lanechange, 
			double length, int color, int number){
		pos = x;
		vel = v;
		this.lane = lane;
		this.model = model;
		this.lanechange = lanechange;
		this.length = length;
		this.color = color;
		nr = number;
	}

	public void accelerate(double dt, Moveable vwd){
		double acc=model.calcAcc(this, vwd);
		vel=vel+acc*dt;
		if (vel<0.0){
			vel=0.0;
		}
	}
	
	public double acceleration(Moveable vwd){
		double acc=model.calcAcc(this, vwd);
		return acc;
	}
	
	public boolean change(Moveable fOld, Moveable fNew, Moveable bNew){
		return lanechange.changeOK(this, fOld, fNew, bNew);
	}
	
	public int color(){return color;}
	
	public double dAcc(Moveable vwd, Moveable bwd){

		double b=bwd.acceleration(this);
		double a=bwd.acceleration(vwd);
		return (b-a);
	}
	
	public int    lane(){return lane;}
	
	public double length(){return length;}
	
	public MicroModel model(){return model;}
	
	public int NR(){return nr;}

	public double position(){return pos;}
	
	public double realLane() { return realLane;}
	
	public void setColor(int color){this.color=color;}
	
	public void setLane(int lane){this.lane=lane;}
	
	public void setLaneChange(LaneChange lanechange){
		this.lanechange=lanechange;}
	
	public void setLaneLast(int lane){this.laneLast=lane;}
	
	public void setLength(double length){this.length=length;}
	
	public void setModel(MicroModel model){this.model=model;}

	public void setPerturbed(){isPerturbed=true;}

	public void setPosition(double x){pos=x;}

	public void setVelocity(double v){vel=v;}

	public boolean timeToChange(double dt){
		tdelay=tdelay+dt;
		if (tdelay>=T_DELAY){
			tdelay=tdelay-T_DELAY;
			laneLast = lane;
			realLane = laneLast;
			return true;
		}
		else {
			realLane = laneLast + (lane - laneLast) * tdelay / T_DELAY;
			return false;
		}
	}

	public void translate(double dt){
		pos=pos+dt*vel;
	}

	public double velocity(){return vel;}
	
}
